package org.aldar.bquest.effects;

import org.aldar.bquest.util.Logger;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.util.math.MathConstants;
import org.andengine.util.modifier.ease.IEaseFunction;

/**
 * User: Daria
 * Date: 1/18/15 11:34 PM
 */
public class MoveSmoothModifier extends MoveModifier {
    private static final Logger LOG = new Logger(MoveSmoothModifier.class);

    public MoveSmoothModifier(float pDuration, float pFromX, float pFromY, float pToX, float pToY) {
        super(pDuration, pFromX, pFromY, pToX, pToY);
    }

    public MoveSmoothModifier(float pDuration, float pFromX, float pFromY, float pToX, float pToY, IEaseFunction pEaseFunction) {
        super(pDuration, pFromX, pFromY, pToX, pToY, pEaseFunction);
    }

    public MoveSmoothModifier(float pDuration, float pFromX, float pFromY, float pToX, float pToY, IEntityModifierListener pEntityModifierListener) {
        super(pDuration, pFromX, pFromY, pToX, pToY, pEntityModifierListener);
    }

    public MoveSmoothModifier(float pDuration, float pFromX, float pFromY, float pToX, float pToY, IEntityModifierListener pEntityModifierListener, IEaseFunction pEaseFunction) {
        super(pDuration, pFromX, pFromY, pToX, pToY, pEntityModifierListener, pEaseFunction);
    }

    protected MoveSmoothModifier(MoveModifier pMoveModifier) {
        super(pMoveModifier);
    }

    @Override
    protected void onManagedUpdate(float pSecondsElapsed, IEntity pItem) {
        super.onManagedUpdate(pSecondsElapsed, pItem);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void onSetValues(IEntity pEntity, float pPercentageDone, float pX, float pY) {
        float appToCenter = 0.7f - Math.abs(0.7f - pPercentageDone);
//        float myX = getFromValueA() + (pPercentageDone + 0.5f * appToCenter) * (getToValueA() - getFromValueA());
//        float myY = getFromValueB() + (pPercentageDone + 0.5f * appToCenter) * (getToValueB() - getFromValueB());
        //float coefficient = (pPercentageDone * pPercentageDone * pPercentageDone)
        //float coefficient = (float) Math.pow((1-(float) Math.cos(pPercentageDone* MathConstants.PI))/2,3f);

//        float coefficient = (float) (pPercentageDone <= 0.5f ?
//                -1 * Math.sqrt((0.5f - pPercentageDone) / 2) + 0.5f
//                : Math.sqrt((pPercentageDone - 0.5f) / 2) + 0.5f);
        ;
//        coefficient = (float) Math.pow(((pPercentageDone - 0.5f) / 4), 1f / 3) + 0.5f;
/*        coefficient = (float) (pPercentageDone <=0.5f?
                -1*(float) Math.pow(((0.5f-pPercentageDone>)/4),1f/3)+0.5f
                :(float) Math.pow(((pPercentageDone-0.5f)/4),1f/3)+0.5f);
  */
        float coefficient = (float) Math.pow((1 - (float) Math.cos(pPercentageDone * MathConstants.PI)) / 2, 3f);

        float myX = getFromValueA() + coefficient * (getToValueA() - getFromValueA());
        float myY = getFromValueB() + coefficient * (getToValueB() - getFromValueB());

        super.onSetValues(pEntity, pPercentageDone, myX, myY);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
